/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.swixml.library;

import javax.swing.Action;

import nemos.swing.IActionProvider;
import nemos.swing.util.ActionFinder;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.osgi.framework.Constants;
import org.osgi.service.component.ComponentContext;
import org.swixml.ConverterLibrary;
import org.swixml.SwingEngine;
import org.swixml.converters.ActionConverter;
import org.swixml.dom.Attribute;

/**
 * ActionServiceConverter : The SwiXML converter for OSGi actions.
 * 
 * <p>
 * This component converts an action ID into an {@link Action}, using the
 * registered {@link IActionProvider} services.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Property(name = Constants.SERVICE_PID, value = "nemos.swing.swixml.Actions")
@Component
public class ActionServiceConverter extends ActionConverter {

    /** The action finder */
    private ActionFinder mActionFinder;

    /**
     * Constructor.
     */
    public ActionServiceConverter() {
        super();
    }

    /**
     * Start the component.
     * 
     * @param pContext
     *            The component context
     */
    @Activate
    public void start(final ComponentContext pContext) {
        mActionFinder = new ActionFinder(pContext.getBundleContext(),
                pContext.getServiceReference());
        ConverterLibrary.getInstance().register(this);
    }

    /**
     * Stop the component.
     */
    @Deactivate
    public void stop() {
        ConverterLibrary.getInstance().register(new ActionConverter());
        mActionFinder.close();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.converters.ActionConverter#convert(java.lang.Class,
     *      org.swixml.dom.Attribute, org.swixml.SwingEngine)
     */
    @Override
    public Action convert(final Class<?> type, final Attribute attr, final SwingEngine<?> engine)
            throws Exception {

        final String lActionId = attr.getValue();
        Action lAction = mActionFinder.findAction(lActionId);

        // Try the basic SwiXML converter method
        if (lAction == null) {
            lAction = super.convert(type, attr, engine);
        }
        return lAction;
    }
}
